package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.zhgd.annotation.ZhjdDataFilter;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.zhgd.util.Query;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.ProductDept;
import com.ruoyi.zhgd.dto.SysStationDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NSysStationBo;
import com.ruoyi.zhgd.domain.vo.NSysStationVo;
import com.ruoyi.zhgd.domain.NSysStation;
import com.ruoyi.zhgd.mapper.NSysStationMapper;
import com.ruoyi.zhgd.service.INSysStationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 拌合楼Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NSysStationServiceImpl implements INSysStationService {

    private final NSysStationMapper baseMapper;

    /**
     * 查询拌合楼
     */
    @Override
    public NSysStationVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询拌合楼列表
     */
    @Override
    public TableDataInfo<NSysStationVo> queryPageList(NSysStationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NSysStation> lqw = buildQueryWrapper(bo);
        Page<NSysStationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询拌合楼列表
     */
    @Override
    public List<NSysStationVo> queryList(NSysStationBo bo) {
        LambdaQueryWrapper<NSysStation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NSysStation> buildQueryWrapper(NSysStationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NSysStation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NSysStation::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NSysStation::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NSysStation::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), NSysStation::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentifier()), NSysStation::getIdentifier, bo.getIdentifier());
        lqw.eq(StringUtils.isNotBlank(bo.getCaptain()), NSysStation::getCaptain, bo.getCaptain());
        lqw.eq(StringUtils.isNotBlank(bo.getCaptainPhone()), NSysStation::getCaptainPhone, bo.getCaptainPhone());
        lqw.eq(bo.getBuyTime() != null, NSysStation::getBuyTime, bo.getBuyTime());
        lqw.eq(bo.getProduceTime() != null, NSysStation::getProduceTime, bo.getProduceTime());
        lqw.eq(StringUtils.isNotBlank(bo.getManufacturer()), NSysStation::getManufacturer, bo.getManufacturer());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), NSysStation::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getService()), NSysStation::getService, bo.getService());
        lqw.eq(StringUtils.isNotBlank(bo.getServicePhone()), NSysStation::getServicePhone, bo.getServicePhone());
        lqw.eq(bo.getSensorConfig() != null, NSysStation::getSensorConfig, bo.getSensorConfig());
        lqw.eq(bo.getMaterialConfig() != null, NSysStation::getMaterialConfig, bo.getMaterialConfig());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), NSysStation::getInfo, bo.getInfo());
        lqw.eq(bo.getCreator() != null, NSysStation::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NSysStation::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NSysStation::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NSysStation::getUpdateDate, bo.getUpdateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleted()), NSysStation::getDeleted, bo.getDeleted());
        lqw.eq(StringUtils.isNotBlank(bo.getIsSmart()), NSysStation::getIsSmart, bo.getIsSmart());
        return lqw;
    }

    /**
     * 新增拌合楼
     */
    @Override
    public Boolean insertByBo(NSysStation bo) {
        ProductDept productDept = baseMapper.selectByBaseCode(bo.getBaseCode());
        SysStationDto sysStationDto = baseMapper.selectByType(bo.getType());
        bo.setName(productDept.getName()+ "-" + sysStationDto.getDictLabel() + "#" + bo.getCode());
        NSysStation add = BeanUtil.toBean(bo, NSysStation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改拌合楼
     */
    @Override
    public Boolean updateByBo(NSysStation bo) {
        NSysStation update = BeanUtil.toBean(bo, NSysStation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NSysStation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除拌合楼
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    @ZhjdDataFilter
    public List<NSysStation> list(Map<String, Object> params) {
        QueryWrapper<NSysStation> queryWrapper = new QueryWrapper<NSysStation>();
        if (ObjectUtils.isNotEmpty(params.get("type"))) {
            queryWrapper.eq("type", params.get("type"));
        }
        if (ObjectUtils.isNotEmpty(params.get("name"))) {
            queryWrapper.like("name", params.get("name"));
        }
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.like("base_code", params.get("baseCode"));
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("code");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @ZhjdDataFilter
    public List<NSysStation> cements(Map<String, Object> params) {
        QueryWrapper<NSysStation> queryWrapper = new QueryWrapper<NSysStation>();
        if (ObjectUtils.isNotEmpty(params.get("type"))) {
            queryWrapper.eq("type", params.get("type"));
        }
        if (ObjectUtils.isNotEmpty(params.get("name"))) {
            queryWrapper.like("name", params.get("name"));
        }
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.like("base_code", params.get("baseCode"));
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("name");
        return baseMapper.selectList(queryWrapper);
    }


    @Override
    public PageUtils sysStationList(Page page, Map<String, Object> params) {
        if(params.get("id") == null){
            params.put("id", LoginHelper.getDeptId().toString());
        }
        List<NSysStationVo> maps = baseMapper.sysStationList(page, params);
        return new PageUtils(page.setRecords(maps));
    }

    @Override
    public List<SysStationDto> sysStationType() {
        return baseMapper.sysStationType();
    }

    /**
     * 根据code 查询拌和站
     *
     * @param stationCode
     * @return
     */
    @Override
    public NSysStation getStationByCode(String stationCode) {
        return baseMapper.getStationByCode(stationCode);
    }

    /**
     * 获取拌和楼列表
     *
     * @param bo
     * @return
     */
    @Override
    public List<NSysStation> allList(NSysStationBo bo) {
        QueryWrapper<NSysStation> queryWrapper = new QueryWrapper<NSysStation>();
        if (ObjectUtils.isNotEmpty(bo.getType())) {
            queryWrapper.eq("type", bo.getType());
        }
        if (ObjectUtils.isNotEmpty(bo.getName())) {
            queryWrapper.like("name", bo.getName());
        }
        if (ObjectUtils.isNotEmpty(bo.getBaseCode())) {
            queryWrapper.like("base_code", bo.getBaseCode());
        }
        queryWrapper.orderByAsc("code");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public R<List<NSysStationVo>> listSelect(NSysStationBo bo) {
        bo.setDeleted("0");
        LambdaQueryWrapper<NSysStation> lqw = buildQueryWrapper(bo);
        List<NSysStationVo> data = baseMapper.selectVoList(lqw);
        return R.ok(data);
    }


}
